<template>
  <div class="homepage-hero-module">
    <div class="video-container">
      <div :style="fixStyle" class="filter"></div>
      <video :style="fixStyle" autoplay loop class="fillWidth"  muted='muted' v-on:canplay="canplay">
        <source src="http://localhost:8888/StudyVideo/getIndexVideo" type="video/mp4"/>
      </video>
      <div class="poster hidden" v-if="!vedioCanPlay">
        <img :style="fixStyle" src="#" alt="">
      </div>
      <div class="box">
        <div><div class="ms-title">学习资源分享系统管理后台</div></div>
        <div class="ms-login">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
            <el-form-item prop="username">
              <el-input v-model="ruleForm.username" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input type="password" v-model="ruleForm.password" placeholder="密码"></el-input>
            </el-form-item>
            <div class="login-btn">
              <el-button type="primary" @click="onSubmit">登录</el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>

</template>

<script>
  export default {
    name: "IndexLogin",
    data() {
      return {
        vedioCanPlay: false,
        fixStyle: '',
        ruleForm:{
          username: "",
          password: ""
        },
        rules:{
          username:[
            {required:true,message:"请输入用户名",trigger:"blur"}
          ],
          password:[
            {required:true,message:"请输入密码",trigger:"blur"}
          ]
        }
      }
    },
    methods: {
      canplay() {
        this.vedioCanPlay = true
      },
      onSubmit(){
        console.log(111)
        var _this=this;
        console.log(this.ruleForm.username)
        this.$axios.post("http://localhost:8888/user/login",
          this.ruleForm)
          .then(function (response) {
            console.log(response)
            if(response.data.msg==="success"){
              _this.$message({
                message: '登录成功!',
                type: 'success'
              });
              localStorage.setItem("adminName",_this.ruleForm.username);
              _this.$router.push("/Home");
            }else{
              _this.$message({
                message: '登录失败!',
                type: 'error'
              });
            }
          })
      },
      goRegister(){
        this.$router.push("/regist");
      }
    },
    mounted: function() {
      window.onresize = () => {
        const windowWidth = document.body.clientWidth
        const windowHeight = document.body.clientHeight
        const windowAspectRatio = windowHeight / windowWidth
        let videoWidth
        let videoHeight
        if (windowAspectRatio < 0.5625) {
          videoWidth = windowWidth
          videoHeight = videoWidth * 0.5625
          this.fixStyle = {
            height: windowWidth * 0.5625 + 'px',
            width: windowWidth + 'px',
            'margin-bottom': (windowHeight - videoHeight) / 2 + 'px',
            'margin-left': 'initial'
          }
        } else {
          videoHeight = windowHeight
          videoWidth = videoHeight / 0.5625
          this.fixStyle = {
            height: windowHeight + 'px',
            width: windowHeight / 0.5625 + 'px',
            'margin-left': (windowWidth - videoWidth) / 2 + 'px',
            'margin-bottom': 'initial'
          }
        }
      }
      window.onresize();
    }
  }
</script>

<style scoped>
  .homepage-hero-module,
  .video-container {
    position: relative;
    height: 100vh;
    overflow: hidden;
  }

  .video-container .poster img,
  .video-container video {
    z-index: 0;
    position: absolute;
  }

  .video-container .filter {
    z-index: 1;
    position: absolute;
    background: rgba(0, 0, 0, 0.4);
  }
  .box{
    position: absolute;
    z-index: 100;
    width: 100%;
    height: 100%;
  }
  .ms-title {
    position: absolute;
    top: 50%;
    width: 100%;
    margin-top: -230px;
    text-align: center;
    font-size: 30px;
    font-weight: 600;
    color: #fff;
  }
  .ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 300px;
    height: 160px;
    margin-left: -190px;
    margin-top: -150px;
    padding: 40px;
    border-radius: 5px;
    background: transparent;
  }
  .login-btn {
    text-align: center;
  }
  .login-btn button {
    width: 100%;
    height: 36px;
  }
</style>
